<?php

namespace app\api\controller;
use fast\Http;
use app\common\controller\Api;
use app\common\library\XdbSearcher;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {

        $sTime = XdbSearcher::now();

        $ip = $this->request->get("ip");
        $cBuff = XdbSearcher::loadContentFromFile("/www/wwwroot/default/application/api/controller/ip2region.xdb");
        $sTime1 = XdbSearcher::now();

        try {
            $searcher = XdbSearcher::newWithBuffer($cBuff);
        } catch (Exception $e) {
            
        }

        $region = $searcher->search($ip);
        $diff = XdbSearcher::now() - $sTime;
        $data = array($region,$sTime1-$sTime,$diff,$ip);
        $this->success('请求成功',$data);
    }

    public function hello(){
        $this->success('请求成功',"hello");
    }


    /** 手表项目使用 */
    public function chatWithAI(){
        $originContent = $this->request->get("content");

        $today = date("Y-m-d H:i:s");
        $content = $originContent . "(当前时间:" . $today . ")" ;
        $preOrder = '请把上面这句话按照如下要求整理，并且按照json格式输出我要求的内容，不要有任何其他多余的东西。' . PHP_EOL
        . '参考示例：' . PHP_EOL 
        . '{' . PHP_EOL 
        .       'now : ' . $today . ',' . PHP_EOL
        .       'content : ' . '事件主体内容，10个字以内,' . PHP_EOL 
        .       'beginTime : '   . '2024-11-01 23:15:00,' . PHP_EOL
        .       'remindTime : '  . '2024-11-01 23:05:00,' . PHP_EOL 
        .       'reason : '      . '解释分析过程' . PHP_EOL
        . '}'
        . '注：' . PHP_EOL 
        . '1. 如果内容中明确提醒时间，请严格使用内容中的时间；' . PHP_EOL 
        . '2. 若 beginTime - now <= 5分钟，remindTime = beginTime；' . PHP_EOL 
        . '3. 若 beginTime - now <= 20分钟，remindTime = beginTime - 5分钟；' . PHP_EOL 
        . '4. 若 beginTime - now > 20分钟，remindTime = beginTime - 15分钟；；)' . PHP_EOL
        . ');' . PHP_EOL
        . "reason" . "说明分析过程" . PHP_EOL
        ;



        $KEY = "sk-j3LeRUwF1JxtT81bMUjJfjlUcir7Sivuxik1tdQhaIHgwE3m";
        $URL = "https://api.moonshot.cn/v1/chat/completions";
        $params = json_encode([
            'model' => 'moonshot-v1-8k',
            'stream' => false,
            'messages' => [
                ['role' => 'system', 'content' => "你现在是我的生活助手，按照我的要求帮我记录日常的代办事项，时间一定要理解正确。"],
                ['role' => 'user', 'content' =>   $content . $preOrder ]
            ],
        ]);

        $res = Http::sendRequest($URL, $params, 'post', [
            CURLOPT_HTTPHEADER => [
                'Content-Type' => 'application/json',
                'Content-Length: ' . strlen($params),
                'Authorization:Bearer ' . $KEY,
            ]
        ]);
        dump($res);
        $result = null;
        if($res['ret']){
            $res = json_decode($res['msg'],true);
            $result = json_decode($res['choices'][0]['message']['content'],true);
            $result['originContent'] = $originContent;
            $result['createTime'] = $today;
        }else{
            $result['originContent'] = $originContent;
        }
        $this->success('请求成功',$result);

    }

    
}
